var express = require("express");
var path = require("path");

var app = express();
var artTemplate = require("express-art-template");

app.use(express.static("public"));

app.engine("html", artTemplate);

app.set("views", path.join(__dirname, "views"));
var mongoose = require("mongoose");

mongoose.connect(
  "mongodb://127.0.0.1:27017/Students",
  {
    useNewUrlParser: true,
    useUnifiedTopology: true
  },
  function(err) {
    if (err) {
      console.log(err);
      console.log("数据库连接失败");
    } else {
      console.log("数据库连接成功");
    }
  }
);

var Student = mongoose.model("stu", {
  index:Number,
  username: String,
  age: Number,
  sex: String,
  zy: String,
  hobby: Array
});
// 添加跳转
app.get('/add',(req, res)=>{
  res.render("add.html");
})

//编辑跳转
app.get('/edit/:aid',(req, res)=>{
  var id=req.params.aid
  var condition={}
 condition._id=id
 Student.find(condition, function(err, content){
  if (err) {
      console.log('查询失败');
  }else{
    console.log("查询成功");
    console.log(content);
    res.render("edit.html", { content });
  }
});

})

//删除
app.get('/delete/:aid',(req, res)=>{
  var id=req.params.aid
 var condition={}
 condition._id=id.substr(1,24)
  Student.deleteOne(condition, function(err){
    if (err) {
      
        console.log('删除失败');
    } else {
       
        console.log('删除成功');
    }
});
res.redirect("/");
})

// 信息添加
app.get("/addstu", function(req, res) {
  // var username=req.query.username
  // var age=req.query.age
  // var sex=req.query.sex
  // var zy=req.query.zy
  // var hobby=req.query.hobby
  var stu = new Student(req.query);
  // 向数据库插入一条数据
  stu.save(function(err) {
    if (err) {
      console.log(err);
      console.log("保存失败");
    } else {
      console.log("保存成功");
    }
  });
  res.redirect("/");
 
});

app.get("/Replay/:aid", function(req, res) {
  var txt = req.query.txt;
  var now = new Date();
  var time1 =
    now.getFullYear() + "年" + now.getMonth() + 1 + "月" + now.getDate() + "日";
  var time2 = now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds();
  var host = req.ip;
  var id=req.params.aid

  Student.updateOne(
    {
      _id: mongoose.mongo.ObjectId(id.substr(1,24))
    },
    {
      $push: {
        replay: { time1: time1, time2: time2, host: host, txt: txt }
      }
    },
    function(err) {
      if (err) {
          console.log(err);
        console.log("更新失败");
        // res.send("更新失败");
      } else {
        // res.send("更新成功");
        console.log("更新成功");
      }
    }
  );
  res.redirect("/");
});

//条件查询
app.get('/query',(req, res)=>{
  var username=req.query.username
  var sex=req.query.sex
  var condition = {};
    if (username) {
        condition.username = username
    }
    if (!sex=='') {
        condition.sex = sex
    }
    console.log(condition);
    Student.find(condition, function(err, content){
      if (err) {
          console.log('查询失败');
      }else{
        console.log("查询成功");
        res.render("index.html", { content });
      }
  }).limit(6);
})

//编辑
app.get('/editstu/:aid',(req, res)=>{
  var id=req.params.aid
  var age=req.query.age
  var sex=req.query.sex
  var zy=req.query.zy
  var hobby=req.query.hobby


  Student.updateOne({
    _id: id.substr(1,24)
}, {
    age:age,
    sex:sex,
    zy:zy,
    hobby:hobby
}, function(err){
    if (err) {
      
        console.log('更新失败');
    } else {
     
        console.log('更新成功');
    }
});

res.redirect("/");
})


var count=0
var nums=0
//下一页
app.get('/next',(req, res)=>{

  Student.countDocuments({}, function(err, num){
    if (err) {
       
        console.log('查询条数失败');
    } else {
        nums=num
        console.log(num+'条数据');
    }
});

 var pages=Math.ceil(nums/6)

var onepages=(count/6)+1





  Student.find(function(err, content) {
    if (err) {
      console.log("查询失败");
    } else {
      console.log("查询成功");
      // console.log(content);
      res.render("index.html", { content,pages,onepages });
    }
  })
  .limit(6)
  .skip(count)
  .exec();
count+=6

if (count>=nums) {
  count=nums-(6*Math.floor(nums))
}
// console.log(count);
})
//上一页
app.get('/prev',(req, res)=>{

  Student.countDocuments({}, function(err, num){
    if (err) {
       
        console.log('查询条数失败');
    } else {
        nums=num
        console.log(num+'条数据');
    }
});

 var pages=Math.ceil(nums/6)
 
var onepages=(count/6)+1
  count-=6
if (count<=0) {
    count=6
  }

  
  Student.find(function(err, content) {
    if (err) {
      console.log("查询失败");
    } else {
      console.log("查询成功");
      // console.log(content);
      res.render("index.html", { content,pages,onepages });
    }
  })
  .limit(6)
  .skip(count)
  .exec();

  

})






// 信息显示
app.get("/", function(req, res) {

  Student.countDocuments({}, function(err, num){
    if (err) {
       
        console.log('查询条数失败');
    } else {
        nums=num
        console.log(num+'条数据');
    }
});

 var pages=Math.ceil(nums/6)
 
 var onepages=(count/6)+1


  Student.find(function(err, content) {
    if (err) {
      console.log("查询失败");
      
    } else {
      console.log("查询成功");
      // console.log(content);
      res.render("index.html", { content,pages,onepages });
    }
  }).limit(6);
});

app.listen(5555, function() {
  console.log("服务器启动...");
});
